package com.sr.examination.news;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.sr.examination.R;
import com.sr.examination.exam.exercise.ExerciseActivity;
import com.sr.examination.exam.exercise.NumberCardActivity;
import com.sr.examination.exam.result.ResultActivity;
import com.sr.examination.net.Net;
import com.tl.commonlibrary.network.ErrorResponse;
import com.tl.commonlibrary.network.RequestListener;
import com.tl.commonlibrary.network.bean.base.BaseBean;
import com.tl.commonlibrary.tool.UIUtils;
import com.tl.commonlibrary.ui.BaseFragment;
import com.tl.commonlibrary.ui.beans.NewsBean;
import com.tl.commonlibrary.ui.widget.pull.PullToRefreshLayout;
import com.tl.commonlibrary.ui.widget.pull.Pullable;

import java.util.ArrayList;

import retrofit2.Call;

/**
 * Created by jiangzhongshan on 18/2/1.
 */

public class NewsListFragment extends BaseFragment {
    public static NewsListFragment get() {
        return new NewsListFragment();
    }

    private ListView newsLView;
    private ArrayList<NewsBean> newses;
    private NewsAdapter adapter;
    private int page = 2;
    private OnLoadNewsListener listener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.frag_news_list, null);
        newsLView = root.findViewById(R.id.newsLView);// ExerciseActivity.start(context);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public void setListener(OnLoadNewsListener listener) {
        this.listener = listener;
    }

    @Override
    public void lazyLoad() {
        super.lazyLoad();
        newsLView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                NewsBean newsBean = adapter.getItem(position);
//                ResultActivity.start(context);
                NewsDetailActivity.start(context, newsBean);
            }
        });
    }

    @Override
    public void onClick(View v) {

    }

    public void setDatas(ArrayList<NewsBean> newsList) {
        this.newses = new ArrayList<>();
        if (newsList != null && !newsList.isEmpty()) {
            newses.addAll(newsList);
        }
        adapter = new NewsAdapter(context, newses);
        newsLView.setAdapter(adapter);

    }

    private void getNewsList() {
        Net.getNewestList(page, new RequestListener<BaseBean<ArrayList<NewsBean>>>() {
            @Override
            public void onSuccess(Call<BaseBean<ArrayList<NewsBean>>> call, BaseBean<ArrayList<NewsBean>> response) {
                if (response.isSuccessful()) {
                    if (response.data != null && !response.data.isEmpty()) {
                        addDatas(response.data);
                        page++;
                        if (listener != null) {
                            listener.onMore(PullToRefreshLayout.SUCCEED);
                        }
                    } else {
                        UIUtils.showToast("没有更多了~");
                        if (listener != null) {
                            listener.onMore(PullToRefreshLayout.SUCCEED);
                        }
                    }
                } else {
                    if (listener != null) {
                        listener.onMore(PullToRefreshLayout.FAIL);
                    }
                }
            }

            @Override
            public void onFailed(Call<BaseBean<ArrayList<NewsBean>>> call, ErrorResponse errorResponse) {

                if (listener != null) {
                    listener.onMore(PullToRefreshLayout.FAIL);
                }
            }
        });
    }

    public void loadMore() {
        getNewsList();
    }

    public interface OnLoadNewsListener {
        void onMore(int finish);

        void onRefersh(int finish);
    }

    public void addDatas(ArrayList<NewsBean> newsList) {
        if (this.newses == null) {
            this.newses = new ArrayList<>();
            if (newsList != null && !newsList.isEmpty()) {
                newses.addAll(newsList);
            }
            adapter = new NewsAdapter(context, newses);
            newsLView.setAdapter(adapter);
        } else {
            if (newsList != null && !newsList.isEmpty()) {
                this.newses.addAll(newsList);
                adapter.notifyDataSetChanged();
            }
        }

    }

}
